
import { generalTrackEvent } from '../api/index'
import { setUuid, getUuid } from './storage'
// 事件枚举eventCode
export const trackingEventMap = {
  activityPageView: {
    //活动页面浏览
    eventCode: "applet_activity0331_page_view",
    requestUrl: "",
    viewPage: "/pages/promotion/promotion",
  },
  activityPageClick: {
    //活动页面点击跳转
    eventCode: "applet_activity0331_page_click",
    requestUrl: "",
    viewPage: "/pages/promotion/promotion",
  },
  getPhone: {
    // 手机号快捷登录-获取手机号
    eventCode: "applet_get_phone",
    requestUrl: "/portal/enterprise/taxes/user/getWechatProPhoneIdByCode",
    viewPage: "/pages/guide/guide",
  },
  phoneAuthLogin: {
    // 手机号快捷登录-登录
    eventCode: "applet_phone_auth_login",
    requestUrl: "/portal/auth/bpai/login",
    viewPage: "/pages/guide/guide",
  },
  phoneAuthCancel: {
    // 手机号快捷登录-取消登录
    eventCode: "applet_phone_auth_cancel",
    requestUrl: "",
    viewPage: "/pages/guide/guide",
  },
  phoneCodeEnter: {
    // 点击手机号验证码登录进入
    eventCode: "applet_phone_code_enter",
    requestUrl: "",
    viewPage: "/pages/guide/guide",
  },
  phoneCodeLogin: {
    // 手机号验证码登录
    eventCode: "applet_phone_code_login",
    requestUrl: "/portal/auth/bpai/login",
    viewPage: "/pages/login/login",
  },
  buyPageView: {
    //购买页面浏览
    eventCode: "applet_buy_page_view",
    requestUrl: "",
    viewPage: "/pages/buy/buy",
  },
  shareForward: {
    //分享转发咨询页面
    eventCode: "applet_share_forward",
    requestUrl: "",
    viewPage: "/pages/chatView/chatView",
  },
  shareForward2: {
    //分享转发生成海报页面
    eventCode: "applet_share_forward",
    requestUrl: "",
    viewPage: "/pages/share/share",
  },
  posterGeneration: {
    //生成海报
    eventCode: "applet_poster_generation",
    requestUrl: "/portal/enterprise/taxes/user/getPicQRCode",
    viewPage: "/pages/share/share",
  },
  loginSmsGet: {
    //获取登录短信验证码成功
    eventCode: "applet_login_sms_get",
    requestUrl: "/portal/enterprise/taxes/user/getWechatProPhoneIdByCode",
    viewPage: "/pages/login/login",
  },
}

function buildUUID() {
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
    const r = (Math.random() * 16) | 0
    const v = c === "x" ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}
// 获取信息
const systemInfo = wx.getDeviceInfo()
console.info(systemInfo)

let localip = ''
wx.getLocalIPAddress({
  success(res) {
    localip = res.localip
  }
})

export const trackingEvent = async (dataObj) => {
  let uuid = null
  if (getUuid()) {
    uuid = getUuid()
  } else {
    uuid = buildUUID()
    setUuid(uuid)
  }

  let data = {
    ip: localip,
    uuid,
    appVersion: systemInfo.brand + '/' + systemInfo.model + '/' + systemInfo.system,
    eventCode: dataObj.eventCode,
    requestUrl: dataObj.requestUrl,
    viewPage: dataObj.viewPage,
  }
  try {
    await generalTrackEvent(data)
  } catch (error) { }
}